package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExtHelper;
import com.ruoyi.common.utils.uuid.IdGeneratorSnowflake;
import com.ruoyi.system.domain.Specialty;
import com.ruoyi.system.mapper.SpecialtyMapper;
import com.ruoyi.system.service.ISpecialtyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 专业Service业务层处理
 *
 * @author dp
 * @date 2023-11-27
 */
@Service
public class SpecialtyServiceImpl implements ISpecialtyService {
    @Autowired
    private SpecialtyMapper specialtyMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;

    /**
     * 查询专业
     *
     * @param specialtyId 专业主键
     * @return 专业
     */
    @Override
    public Specialty selectSpecialtyBySpecialtyId(Long specialtyId) {
        return specialtyMapper.selectSpecialtyBySpecialtyId(specialtyId);
    }

    /**
     * 查询专业列表
     *
     * @param specialty 专业
     * @return 专业
     */
    @Override
    public List<Specialty> selectSpecialtyList(Specialty specialty) {
        return specialtyMapper.selectSpecialtyList(specialty);
    }

    /**
     * 新增专业
     *
     * @param specialty 专业
     * @return 结果
     */
    @Override
    public int insertSpecialty(Specialty specialty) {
        specialty.setCreateTime(DateUtils.getNowDate());
        return specialtyMapper.insertSpecialty(specialty);
    }

    /**
     * 修改专业
     *
     * @param specialty 专业
     * @return 结果
     */
    @Override
    public int updateSpecialty(Specialty specialty) {
        return specialtyMapper.updateSpecialty(specialty);
    }

    /**
     * 批量删除专业
     *
     * @param specialtyIds 需要删除的专业主键
     * @return 结果
     */
    @Override
    public int deleteSpecialtyBySpecialtyIds(Long[] specialtyIds) {
        return specialtyMapper.deleteSpecialtyBySpecialtyIds(specialtyIds);
    }

    /**
     * 删除专业信息
     *
     * @param specialtyId 专业主键
     * @return 结果
     */
    @Override
    public int deleteSpecialtyBySpecialtyId(Long specialtyId) {
        return specialtyMapper.deleteSpecialtyBySpecialtyId(specialtyId);
    }

    /**
     * 实体数据补全
     *
     * @param specialty 实体
     * @param userId    用户id
     * @param crateType 创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(Specialty specialty, Long userId, Integer crateType) {
        //主键
        if (specialty.getSpecialtyId() == null || "".equals(specialty.getSpecialtyId())) {
            specialty.setSpecialtyId(ExtHelper.toString(idGeneratorSnowflake.nextId()));
        }
        //创建人、创建时间
        if (crateType == 1) {
            if (specialty.getCreateBy() == null) {
                specialty.setCreateBy(userId.toString());
            }
            if (specialty.getCreateTime() == null) {
                specialty.setCreateTime(DateUtils.getNowDate());
            }
        }
        //修改人
        if (specialty.getModifyBy() == null) {
            specialty.setModifyBy(userId);
        }
        //修改时间
        if (specialty.getModifyTime() == null) {
            specialty.setModifyTime(DateUtils.getNowDate());
        }
    }
}
